package main

import (
	"cat/consult/protocol"
	"fmt"
	"io"
	"log"
	"net"
)

func main() {
	listen, err := net.Listen("tcp", "127.0.0.1:19998")
	if err != nil {
		log.Fatalf("listen err=%v", err)
		return
	}

	defer listen.Close()

	for {
		conn, err := listen.Accept()
		if err != nil {
			log.Fatalf("accept err=%v", err)
			return
		}
		go handle(conn)
	}
}

func handle(conn net.Conn) {
	defer conn.Close()
	reader := io.Reader(conn)

	for {
		context, err := protocol.Decode(reader)
		if err == io.EOF {
			log.Fatalf("conn finish")
			return
		}

		if err != nil {
			log.Fatalf("decode err=%v", err)
		}

		fmt.Println("client ", context)
	}

}
